package com.example.stream;

import java.util.Arrays;
import java.util.List;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @author wangyingjie
 * @date
 * @remark 数值流
 */
public class NumberDemo {

    public static void main(String[] args) {
        List<Dish> menu = Arrays.asList(
                new Dish("pork", false, 800, Dish.Type.MEAT),
                new Dish("beef", false, 700, Dish.Type.MEAT),
                new Dish("chicken", false, 400, Dish.Type.MEAT),
                new Dish("french fries", true, 530, Dish.Type.OTHER),
                new Dish("rice", true, 350, Dish.Type.OTHER),
                new Dish("season fruit", true, 120, Dish.Type.OTHER),
                new Dish("pizza", true, 550, Dish.Type.OTHER),
                new Dish("prawns", false, 300, Dish.Type.FISH),
                new Dish("salmon", false, 450, Dish.Type.FISH) );
        //转换成数值流，调用特定的方法
        int sum = menu.stream().mapToInt(Dish::getCalories).sum();
        OptionalDouble avg = menu.stream().mapToInt(Dish::getCalories).average();
        OptionalInt max = menu.stream().mapToInt(Dish::getCalories).max();
        int i = max.orElse(1);//如果没有最大值就设置1
        //数值流转回对象流
        IntStream intStream = menu.stream().mapToInt(Dish::getCalories);
        Stream<Integer> boxed = intStream.boxed();
        //生成1到100的偶数
        IntStream intStream1 = IntStream.rangeClosed(1, 100).filter(e -> e % 2 == 0);
        System.out.println(intStream1.count());//个数
        //值构造流
        Stream<String> stream = Stream.of("Java", "8", "in", "action");
        stream.map(String::toLowerCase).forEach(System.out::println);
        //构造空流
        Stream<Object> empty = Stream.empty();
        //无线流
        Stream<Integer> iterate = Stream.iterate(0, e -> e + 2).limit(10);
        //打印斐波那契数列
        Stream.iterate(new int[]{0,1},e->{
          return   new int[]{e[1],e[0]+e[1]};
        }).limit(20).map(e->e[0]).forEach(System.out::println);

    }

}
